let DBConfig = require("../config/DBConfig");

module.exports = {
    //返回所有数据
    async rInfo(req, resp) {
        let userInfo = await new Promise(((resolve, reject) => {
            DBConfig.db('SELECT * FROM ordertable,physical WHERE ordertable.phy_id=physical.phy_id', [], (err, data) => {
                if (data.length > 0) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }));
        resp.send(userInfo);
    },
    //查询数据
    async rSearchINfo(req, resp) {
        let {value, selectValue, time} = req.body;

        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db("SELECT * FROM ordertable WHERE (user_name LIKE ? OR user_tel LIKE ? OR idCard LIKE ?)  or order_status like ? or  order_date = ?", [`%${value}%`, `%${value}%`, `%${value}%`, `${selectValue}`, time], (err, data) => {
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        });
        resp.send(userInfo)
    },
    //编辑用户数据
    async rEditInfo(req, resp) {
        let {orderId, cTime} = req.body;
        let userInfo = await new Promise(((resolve, reject) => {
            DBConfig.db('update ordertable set order_date = ? where order_orderId = ?', [cTime, orderId], (err, data) => {
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }))
        resp.send(userInfo);
    },
    //编辑用户状态
    async rEditStatus(req, resp) {
        let {orderId, status} = req.body;
        let userInfo = await new Promise(((resolve, reject) => {
            DBConfig.db('update ordertable set order_status = ? where order_orderId = ?', [status, orderId], (err, data) => {
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }))
        resp.send(userInfo);
    },
    //体检加项的内容
    async p_addInfo(req, resp) {
        let {productId} = req.query;
        let userInfo = await new Promise((resolve, reject) => {
            //查询套餐id里面有多少个项目，除了这些项目id的就是加项
            DBConfig.db('select phy_id from set_meal where productId = ?', [productId], (err, data) => {
                if (!err) {
                    resolve(data);
                } else {
                    reject(err)
                }
            })
        }).then(res => {
            let {phy_id} = res[0]
            let search_id = phy_id.split(',');
            let newArr = search_id.map(item => {
                return parseInt(item)
            });
            return new Promise((resolve, reject) => {
                DBConfig.db(`select * from physical where phy_id not in(${newArr.join(',')})`, [], (err, data) => {
                    if (!err) {
                        resolve(data);
                    } else {
                        reject(err);
                    }
                })
            })

        })
        resp.send(userInfo);
    },
    //根据套餐id查体检加项
    async rSelectAdd(req, resp) {
        let {productId, sel} = req.query;
        let userInfo = await new Promise((resolve, reject) => {
            //查询套餐id里面有多少个项目，除了这些项目id的就是加项
            DBConfig.db('select * from set_meal where productId = ?', [productId], (err, data) => {
                if (!err) {
                    resolve(data);
                } else {
                    reject(err)
                }
            })
        }).then(res => {
            return new Promise((resolve, reject) => {
                DBConfig.db(`select * from physical where phy_id in(${sel})`, [], (err, data) => {
                    if (!err) {
                        resolve({data, res});
                    } else {
                        reject(err);
                    }
                })
            })

        })
        resp.send(userInfo);
    },
    //只查套餐
    async getMeal(req, resp) {
        let {productId, sel} = req.query;
        let userInfo = await new Promise((resolve, reject) => {
            //查询套餐id里面有多少个项目，除了这些项目id的就是加项
            DBConfig.db('select * from set_meal where productId = ?', [productId], (err, data) => {
                if (!err) {
                    resolve(data);
                } else {
                    reject(err)
                }
            })
        })
        resp.send(userInfo)
    },
    //查询所有体检机构
    async getOrg(req, resp) {
        let userInfo = await new Promise((resolve, reject) => {
            //查询套餐id里面有多少个项目，除了这些项目id的就是加项
            DBConfig.db('select * from organization', [], (err, data) => {
                if (!err) {
                    resolve(data);
                } else {
                    reject(err)
                }
            })
        })
        resp.send(userInfo)
    },
    //添加支付订单
    async addReserveOrder(req, resp) {
        let {order_orderId, user_id, productId, phy_id, order_status, order_mongy, order_payWays, addPrice,order_date} = req.query;
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('select user_id,user_name,gender,user_tel,idCard from register where user_id=?', [parseInt(user_id)], (err, data) => {
                if (!err) {
                    resolve(data);
                }
            })
        }).then(res => {
            return new Promise((resolve, reject) => {
                DBConfig.db('select productId,com_salesPrice,org_name from set_meal', [], (err, data) => {
                    if (!err) {
                        resolve({meal:data[0],user:res[0]});
                    } else {
                        reject(err);
                    }
                })
            })
        })
            .then(res2 => {
                return new Promise((resolve, reject) => {
                    let newArr = phy_id.split(',');
                    let newArr2 = [];
                        newArr.map(item=>{
                            newArr2.push(parseInt(item))
                    })
                    let phyData = [];
                    newArr2.map((item,index)=>{
                        DBConfig.db(`select phy_projectName from physical where phy_id = ?`, [item], (err, data) => {
                            if (!err) {
                                phyData.push(data[0]);
                                if(index===newArr2.length-1){
                                    resolve({addINfo:phyData, user:res2.user,meal:res2.meal});
                                }

                            } else {
                                reject(err);
                            }
                        })
                    })

                })
            }).then(res => {
                console.log(res)
                return new Promise((resolve, reject) => {
                    let {user_id,user_name,gender,user_tel,idCard} = res.user;
                    let {productId,org_name,com_salesPrice} = res.meal;
                    let addName = [];
                    res.addINfo.map(item=>{
                        addName.push(item.phy_projectName);
                    })
                    let projectName = addName.join(',');
                    if(order_payWays==="wallet"){
                       order_payWays = '钱包';
                    }else if(order_payWays==="alipay"){
                       order_payWays = '支付宝';
                    }else if(order_payWays==="wechat"){
                       order_payWays = '微信';
                    }else if(order_payWays==="online"){
                       order_payWays = '网银';
                    }
                    DBConfig.db('insert into ordertable values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', [user_id,user_name,gender,user_tel,idCard,productId,com_salesPrice,phy_id,addPrice,org_name,order_date,order_orderId,order_status,order_mongy,order_payWays,projectName], (err, data) => {
                        if(!err){
                            resolve({code:200,msg:'成功'})
                        }else{
                            reject(err);
                        }
                    })
                })
            })
        resp.send(userInfo);
    },
    async hasPayAdd(req,resp){
        let {order_id} = req.query;
        let userInfo = await new Promise((resolve, reject) => {
            //查询套餐id里面有多少个项目，除了这些项目id的就是加项
            DBConfig.db('select phy_id from ordertable where order_id = ?', [parseInt(order_id)], (err, data) => {
                if (!err) {
                    let newArr = data[0].phy_id.split(',').map(item=>{
                        return parseInt(item);
                    });
                    let newArr2 = newArr.join(',');
                    resolve(newArr2);
                } else {
                    reject(err)
                }
            })
        }).then(res=>{
            console.log(res)
            return new Promise((resolve, reject) => {
                DBConfig.db(`select * from physical where phy_id not in(${res})`, [], (err, data) => {
                    if (!err) {
                        resolve(data);
                    } else {
                        reject(err);
                    }
                })
            })
        })
        console.log(userInfo)
        resp.send(userInfo);
    },
    async hasSelectAdd(req,resp){
        let {sel} = req.query;
        let userINfo = await new Promise((resolve, reject) => {
            DBConfig.db(`select * from physical where phy_id  in(${sel})`, [], (err, data) => {
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        })
        resp.send(userINfo)
    },
    async hasSelectOrder(req,resp){
        let{order_id} = req.query;
        let userINfo = await new Promise((resolve, reject) => {
            DBConfig.db(`select * from ordertable where order_id=?`, [order_id], (err, data) => {
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        })
        resp.send(userINfo)
    },
    async hasUpOrderAdd(req,resp){
        let{order_id,price,payway} = req.query;
        let userINfo = await new Promise((resolve, reject) => {
            DBConfig.db(`update ordertable set order_money=?,order_payWays=? where order_id=?`, [price,payway,order_id], (err, data) => {
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        })
        resp.send(userINfo)
    },
    async hasPay(req,resp){
        let {order_id,add_projetName,sel,payway,price} = req.query;
        let userINfo = await new Promise((resolve, reject) => {
            DBConfig.db(`select phy_projectName,phy_id,addPrice,order_money from ordertable where order_id=?`, [order_id], (err, data) => {
                if (!err) {

                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).then(res=>{
            let phy_projectName = res[0].phy_projectName+','+add_projetName;
            let phy_id = res[0].phy_id+','+sel;
            let addPrice = parseInt(res[0].addPrice)+parseInt(price);
            console.log(addPrice,payway,phy_id,phy_projectName,order_id)
            return new Promise((resolve, reject) => {
                DBConfig.db(`update ordertable set addPrice=?,order_payWays=?,phy_id=?,phy_projectName=? where order_id=?`, [addPrice,payway,phy_id,phy_projectName,order_id], (err, data) => {
                    if (!err) {
                        resolve(data);
                    } else {
                        reject(err);
                    }
                })
            })
        })
    },
    async noPay(req,resp){
        let{order_id,price,payway} = req.query;
        console.log(order_id,price,payway);
        let userINfo = await new Promise((resolve, reject) => {
            DBConfig.db(`update ordertable set order_money=?,order_payWays=? where order_id=?`, [price,payway,order_id], (err, data) => {
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        })
        resp.send(userINfo)
    },
async selectUserInfo(req, resp) {
    let { userName } = req.body;

    let userInfo = await new Promise((resolve, reject) => {
        DBConfig.db("select * from categorys", [], (err, data) => {
            if (data.length > 0) {
                resolve(data);
            } else {
                reject(err);
            }
        });
    });

    resp.send(userInfo);
},

};



